<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <title>物料维护</title>
    <link rel="stylesheet" href="../../../../public/dist/css/layui.css">
</head>

<body>
    <form class="layui-form" action="" id="example" lay-filter="example">
        <div class="select common">
            <div class="select-title"> 物料维护</div>
            <div class="select-con">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">创建时间:</label>
                        <div class="layui-input-inline">
                            <input type="text" id="tesss" class="layui-input input-d   ming" name="createTime" placeholder="输入时间">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">物料名称:</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input input-d  house" placeholder="输入物料名称" name="description">
                        </div>
                    </div>
                </div>


                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">主仓库:</label>
                        <div class="layui-input-inline ">
                            <select name="locationId" lay-filter="locationId" class="locationId">
                                <option value="">---请选择---</option>
                            </select>
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">供应商代号:</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input input-d  daihao" placeholder="输入供应商代号" name="vendorId" disabled>
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">供应商:</label>
                        <div class="layui-input-inline ">
                            <select name="locationName" lay-filter="locationName" class="locationName">
                                <option value="">---请选择---</option>
                            </select>
                        </div>
                    </div>
                </div>


                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">物料类别:</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input input-d  leibie" placeholder="输入物料类别" name="productCode" disabled>
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">类别名称:</label>
                        <div class="layui-input-inline ">
                            <select name="productCodeName" lay-filter="productCodeName" class="productCodeName">
                                <option value="">---请选择---</option>
                            </select>
                        </div>
                    </div>
                </div>

                <!-- <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">库存:</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input input-d  qtyOnHand" placeholder="输入库存" name="qtyOnHand" disabled>
                        </div>
                    </div>
                </div> -->

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">规格:</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input input-d  specification" placeholder="输入规格" name="specification">
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">参考进价:</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input input-d  unitBuyCost" placeholder="输入参考进价" name="unitBuyCost">
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">参考售价:</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input input-d  unitSaleCost" placeholder="输入参考售价" name="unitSaleCost">
                        </div>
                    </div>
                </div>


                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">计量单位:</label>
                        <div class="layui-input-inline ">
                            <select name="unitName" lay-filter="unitName" class="unitName">
                                <option value="">---请选择---</option>
                            </select>
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">来源:</label>
                        <div class="layui-input-inline ">
                            <select name="vendorName" lay-filter="vendorName" class="vendorName">
                                <option value="">---请选择---</option>
                                <option value="采购">采购</option>
                                <option value="自制">自制</option>
                                <option value="委外">委外</option>
                                <option value="来料">来料</option>
                            </select>
                        </div>
                    </div>
                </div>


                <div class="layui-form-item  xiu">
                    <div class="layui-inline">
                        <label class="layui-form-label">修改时间:</label>
                        <div class="layui-input-inline">
                            <input type="text" id="tesss1" class="layui-input input-d " placeholder="输入时间">
                        </div>
                    </div>
                </div>

                <div class="select-btn" style="margin-left:30px;">
                    <div class="layui-bg-green comquery queding" id="queding">确定</div>
                    <div class="layui-bg-green comquery cancels">取消</div>
                </div>
            </div>
        </div>
    </form>
</body>
<script src="../../../../js/module/jquery-3.4.1.min.js"></script>
<script src="../../../../public/dist/layui.js" charset="utf-8"></script>
<script src="../../../../js/common/api.js"></script>

</html>

<script>
    layui.use(["table", "laydate", "form"], function() {
        var $ = layui.jquery,
            form = layui.form,
            laydate = layui.laydate,
            obj = {},
            table = layui.table;

        laydate.render({
            elem: '#tesss' //指定元素
        });
        laydate.render({
            elem: '#tesss1' //指定元素
        });

        //取消
        $(".cancels").click(function() {
            window.location.href = "../materialmaintain.html";
        });



        //仓库
        obj.locationId = "";
        noAjax("coa/getLocationList", {}, "get").done(function(resp) {
            $.each(resp.ipage.records, function(v, item) {
                $(".locationId").append('<option  value="' + item.id + '">' + item.description + '</option>');
            })
            form.render('select');
            return
        }).fail(function(err) {
            console.log(err)
        });
        form.on('select(locationId)', function(data) {
            obj.locationId = data.value;
            return
        });


        //供应商 
        obj.locationName = "";
        obj.vendorId = "";
        noAjax("coa/getSuppliersList", {}, "get").done(function(resp) {
            $.each(resp.ipage.records, function(v, item) {
                $(".locationName").append('<option  value="' + item.vendorId + '">' + item.fullName + '</option>');
            })
            form.render('select');
            return
        }).fail(function(err) {
            console.log(err)
        });
        form.on('select(locationName)', function(data) {
            obj.locationName = $(this)[0].innerText;
            obj.vendorId = data.value;
            $(".daihao").val(data.value)
            return
        });


        //物料类别
        obj.productCode = "";
        obj.productCodeName = "";
        noAjax("coa/getProductTypeList", {}, "get").done(function(resp) {
            console.log(resp)
            $.each(resp.ipage.records, function(v, item) {
                $(".productCodeName").append('<option  value="' + item.productCode + '">' + item.description + '</option>');
            })
            form.render('select');
            return
        }).fail(function(err) {
            console.log(err)
        });
        form.on('select(productCodeName)', function(data) {
            obj.productCodeName = $(this)[0].innerText;
            obj.productCode = data.value;
            $(".leibie").val(data.value);
            return
        });


        //计量单位
        obj.unitName = "";
        noAjax("coa/getUnitsList", {}, "get").done(function(resp) {
            console.log(resp)
            $.each(resp.ipage.records, function(v, item) {
                $(".unitName").append('<option  value="' + item.description + '">' + item.description + '</option>');
            })
            form.render('select');
            return
        }).fail(function(err) {
            console.log(err)
        });
        form.on('select(unitName)', function(data) {
            obj.unitName = data.value;
            return
        });


        //来源
        obj.vendorName = "";
        form.on('select(vendorName)', function(data) {
            obj.vendorName = data.value;
            return
        });


        //编辑
        var urls = window.location.search;
        urls = urls.substring(4);
        console.log(urls)
        noAjax("coa/getProductList", {
            id: urls
        }, "get").done(function(resp) {
            console.log(resp.ipage.records);
            if (urls != "") {
                $(".xiu").show();
                var ss = resp.ipage.records[0].createTime;
                resp.ipage.records[0].createTime = ss.substring(0, 10);
            } else {
                $(".xiu").hide();
            }
            form.val("example", resp.ipage.records[0])
            $('.unitName').val('你的value值');
            form.render('select', "unitName");
            form.render('select', "locationName");
            form.render('select', "productCodeName");
            form.render('select', "unitName");
            return
        }).fail(function(err) {
            console.log(err)
        });



        $("#queding").click(function() {

            if ($("#tesss").val() == "") {
                alerts("请选择时间")
                return
            }
            if ($(".house").val() == "") {
                alerts("请输入物料名称")
                return
            }

            obj.createTime = $("#tesss").val() + " " + "00:00:00";
            obj.description = $(".house").val();
            obj.qtyOnHand = "";
            obj.specification = $(".specification").val();
            obj.unitBuyCost = $(".unitBuyCost").val();
            obj.unitSaleCost = $(".unitSaleCost").val();


            if (urls != "") {
                obj.id = urls;
                obj.editTime = $("#tesss1").val() + " " + "00:00:00";
                handleAjax("coa/updateProduct", obj, "POST").done(function(resp) {
                    layer.msg('操作成功', {
                        icon: 1,
                        time: 1000
                    });
                    setTimeout(function() {
                        window.location.href = "../materialmaintain.html";
                    }, 1500)
                    return
                }).fail(function(err) {
                    console.log(err)
                });

            } else {
                obj.editTime = "";
                handleAjax("coa/addProduct", obj, "POST").done(function(resp) {
                    layer.msg('操作成功', {
                        icon: 1,
                        time: 1000
                    });
                    setTimeout(function() {
                        window.location.href = "../materialmaintain.html";
                    }, 1500)
                    return
                }).fail(function(err) {
                    console.log(err)
                });
            }

        });
    });
</script>